package cn.wildfirechat.message;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.wildfirechat.message.core.ContentTag;
import cn.wildfirechat.message.core.MessageContentType;
import cn.wildfirechat.message.core.MessagePayload;
import cn.wildfirechat.message.core.PersistFlag;
import cn.wildfirechat.message.notification.NotificationMessageContent;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.remote.ChatManager;

/**
 * 撤回全部
 */

@ContentTag(type = MessageContentType.REMOVAL_OF_CHAT, flag = PersistFlag.Persist)
public class WithdrawAllContent extends NotificationMessageContent {
    private String fromUser;
    private String mids;

    public WithdrawAllContent() {
    }

    @Override
    public MessagePayload encode() {
        MessagePayload messagePayload = new MessagePayload();
        try {
            JSONObject objWrite = new JSONObject();
            objWrite.put("fromUser", fromUser);
            objWrite.put("mids", mids);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return messagePayload;
    }

    @Override
    public void decode(MessagePayload payload) {
        if (!TextUtils.isEmpty(payload.content)) {
            try {
                if (payload.content != null) {
                    JSONObject jsonObject = new JSONObject(payload.content);
                    fromUser = jsonObject.optString("fromUser");
                    mids = jsonObject.optString("mids");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    protected WithdrawAllContent(Parcel in) {
        super(in);
        this.fromUser = in.readString();
        this.mids = in.readString();
    }


    @Override
    public String formatNotification(Message message) {

        String displayName;
        if (fromSelf) {
            displayName = "您";
        } else {
            if (message.conversation.type == Conversation.ConversationType.Group) {
                displayName = ChatManager.Instance().getGroupMemberDisplayName(message.conversation.target, fromUser);
            } else {
                displayName = ChatManager.Instance().getUserDisplayName(fromUser);
            }
        }
        return displayName + "撤回了全部消息";
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.fromUser);
        dest.writeString(this.mids);

    }

    public static final Creator<WithdrawAllContent> CREATOR = new Creator<WithdrawAllContent>() {
        @Override
        public WithdrawAllContent createFromParcel(Parcel source) {
            return new WithdrawAllContent(source);
        }

        @Override
        public WithdrawAllContent[] newArray(int size) {
            return new WithdrawAllContent[size];
        }
    };

    public String getFromUser() {
        return fromUser;
    }

    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }

    public String getMids() {
        return mids;
    }

    public void setMids(String mids) {
        this.mids = mids;
    }
}
